
package upload;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oreilly.servlet.MultipartRequest;
import common.CommonHttpServlet;

/**
 * url:http://127.0.0.1:8080/JSP/upload/basicUpload.jsp<br>
 * 使用 oreilly(cos.jar) 來上傳檔案<br>
 * PS: 上傳檔案時，檔案名不可用中文，不然會出現亂碼，目前無法解決，可以用 RenameUploadServlet 的方式解決<br>
 * 
 * @author Eric.Kao(gau168.gau@msa.hinet.net)
 * @version 1.0
 * @company E885 Group
 * @copyright Copyright (c) 2001-2008
 */
public class BasicUploadServlet extends CommonHttpServlet
{
	private static final long serialVersionUID = -177416227732035033L;

	private String SAVE_DIRECTORY = "upload";

	protected void service( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
	{
		String realPath = getServletContext().getRealPath( SAVE_DIRECTORY );

		File file = new File( realPath );

		if( !file.exists() )
			file.mkdir();

		MultipartRequest mr = new MultipartRequest( request, realPath );

		String fileName = mr.getFilesystemName( "uploadFile" );
		
		PrintWriter writer = response.getWriter();
		writer.write( "upload success, file name:" + fileName );
	}
}
